package com.ruoyi.web.controller.userComment;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.review.domain.Comment;
import com.ruoyi.review.service.ICommentService;
import com.ruoyi.system.domain.UserCard;
import com.ruoyi.system.domain.UserComment;
import com.ruoyi.system.service.IUserCommentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户评论关联（允许重复关联）Controller
 * 
 * @author ruoyi
 * @date 2025-06-04
 */
@RestController
@RequestMapping("/userComment/userComment")
public class UserCommentController extends BaseController
{
    @Autowired
    private IUserCommentService userCommentService;
    @Autowired
    private ICommentService commentService;
    /**
     * 查询用户评论关联（允许重复关联）列表
     */
    @PreAuthorize("@ss.hasPermi('userComment:userComment:list')")
    @GetMapping("/list")
    public TableDataInfo list(UserComment userComment)
    {
        startPage();
        List<UserComment> list = userCommentService.selectUserCommentList(userComment);
        return getDataTable(list);
    }

    /**
     * 导出用户评论关联（允许重复关联）列表
     */
    @PreAuthorize("@ss.hasPermi('userComment:userComment:export')")
    @Log(title = "用户评论关联（允许重复关联）", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, UserComment userComment)
    {
        List<UserComment> list = userCommentService.selectUserCommentList(userComment);
        ExcelUtil<UserComment> util = new ExcelUtil<UserComment>(UserComment.class);
        util.exportExcel(response, list, "用户评论关联（允许重复关联）数据");
    }

    /**
     * 获取用户评论关联（允许重复关联）详细信息
     */
    @PreAuthorize("@ss.hasPermi('userComment:userComment:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(userCommentService.selectUserCommentById(id));
    }

    /**
     * 新增用户评论关联（允许重复关联）
     */
    @PreAuthorize("@ss.hasPermi('userComment:userComment:add')")
    @Log(title = "用户评论关联（允许重复关联）", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody UserComment userComment)
    {
        return toAjax(userCommentService.insertUserComment(userComment));
    }

    /**
     * 修改用户评论关联（允许重复关联）
     */
    @PreAuthorize("@ss.hasPermi('userComment:userComment:edit')")
    @Log(title = "用户评论关联（允许重复关联）", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody UserComment userComment)
    {
        return toAjax(userCommentService.updateUserComment(userComment));
    }

    /**
     * 删除用户评论关联（允许重复关联）
     */
    @PreAuthorize("@ss.hasPermi('userComment:userComment:remove')")
    @Log(title = "用户评论关联（允许重复关联）", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(userCommentService.deleteUserCommentByIds(ids));
    }
//    获取当前用户的评论

    @GetMapping("/Comments")
    public TableDataInfo Comments(){
        Long userId = SecurityUtils.getUserId();
//        System.out.println(userId);
        UserComment userComment = new UserComment();
        userComment.setUserId(userId);
        List<UserComment> userComments =userCommentService.selectUserCommentList(userComment);
//        System.out.println(userComments);
        ArrayList<Comment> comments = new ArrayList<>();
        for (UserComment comment : userComments) {
            Long commentId = comment.getCommentId();
            Comment comment1 = commentService.selectCommentById(commentId);
            comments.add(comment1);
        }
        return getDataTable(comments);
    }
}
